package com.zbkj.crmeb.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.constants.Constants;
import com.zbkj.crmeb.front.request.UpdateDto;
import com.zbkj.crmeb.front.service.UpdateService;
import com.zbkj.crmeb.share.dao.TempShareIpDao;
import com.zbkj.crmeb.share.model.TempShareIp;
import com.zbkj.crmeb.system.request.SystemFormTempSearchRequest;
import com.zbkj.crmeb.system.service.SystemConfigService;
import com.zbkj.crmeb.version.dao.VersionDao;
import com.zbkj.crmeb.version.model.VersionRaw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UpdateServiceImpl implements UpdateService {
    @Value("${apk.androidUrl}")
    private String androidUrl;
    @Value("${apk.iosUrl}")
    private String iosUrl;

    @Autowired
    TempShareIpDao tempShareIpDao;

    @Autowired
    VersionDao versionDao;

    @Autowired
    SystemConfigService systemConfigService;

    @Override
    public void createTempIpRecord(String spread, String clientIp) {
        TempShareIp results = tempShareIpDao.selectOne(new LambdaQueryWrapper<TempShareIp>().eq(TempShareIp::getRequestIp, clientIp));
        TempShareIp tempShare;
        Boolean isUpdate = false;
        if (results==null) {
            tempShare = new TempShareIp();
        } else {
            tempShare = results;
            isUpdate = true;
        }

        tempShare.setSpread(spread);
        tempShare.setRequestIp(clientIp);


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, 30);
        tempShare.setExpireTime(calendar.getTime());
        if (isUpdate) {
            tempShareIpDao.updateById(tempShare);
        } else {
            tempShareIpDao.insert(tempShare);
        }

    }

    @Override
    public void cleanExpiredTempShare() {
        tempShareIpDao.delete(new LambdaQueryWrapper<TempShareIp>().le(TempShareIp::getExpireTime, new Date()));
    }

    @Override
    public UpdateDto.UpdateResp getUpdateInfo(UpdateDto.UpdateReq req) {
        VersionRaw updateRaw = versionDao.selectOne(new LambdaQueryWrapper<VersionRaw>().eq(VersionRaw::getIsLatest, true));
        UpdateDto.UpdateResp updateResp = new UpdateDto.UpdateResp();
        if (updateRaw == null || req.getVersion().equals(genVersion(updateRaw))) {
            updateResp.setIsUpdate(false);
        } else {
            String appPath = systemConfigService.getValueByKey(Constants.CONFIG_APP_PATH);
            if (!StringUtils.isEmpty(appPath)) {
                updateResp.setAndroidUrl(appPath + "android.apk");
                updateResp.setIosUrl(appPath + "ios.apk");
            } else {
                updateResp.setAndroidUrl(androidUrl);
                updateResp.setIosUrl(iosUrl);
            }

            updateResp.applyTo(updateRaw);
            updateResp.setIsUpdate(true);
        }

        return updateResp;
    }

    private String genVersion(VersionRaw updateRaw) {
        return updateRaw.getMainVersion() + "." + updateRaw.getMiniVersion() + "." + updateRaw.getResourceVersion();
    }
}
